home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / dired.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-18  |  22.7 KB  |  747 lines

  1.  /* Lisp functions for making directory listings.
  2.    Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.28. */
  21.  
  22. #include <config.h>
  23. #include "lisp.h"
  24.  
  25. #include "buffer.h"
  26. #include "commands.h"
  27. #include "elhash.h"
  28. #include "regex.h"
  29.  
  30. #include "sysfile.h"
  31. #include "sysdir.h"
  32.  
  33. Lisp_Object Vcompletion_ignored_extensions;
  34.  
  35. Lisp_Object Qdirectory_files;
  36. Lisp_Object Qfile_name_completion;
  37. Lisp_Object Qfile_name_all_completions;
  38. Lisp_Object Qfile_attributes;
  39.  
  40. DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 5, 0,
  41.   "Return a list of names of files in DIRECTORY.\n\
  42. There are four optional arguments:\n\
  43. If FULL is non-nil, absolute pathnames of the files are returned.\n\
  44. If MATCH is non-nil, only pathnames containing that regexp are returned.\n\
  45. If NOSORT is non-nil, the list is not sorted--its order is unpredictable.\n\
  46.  NOSORT is useful if you plan to sort the result yourself.\n\
  47. If FILES-ONLY is the symbol t, then only the \"files\" in the directory\n\
  48.  will be returned; subdirectories will be excluded.  If FILES-ONLY is not\n\
  49.  nil and not t, then only the subdirectories will be returned.  Otherwise,\n\
  50.  if FILES-ONLY is nil (the default) then both files and subdirectories will\n\
  51.  be returned.")
  52.   (dirname, full, match, nosort, files_only)
  53.      Lisp_Object dirname, full, match, nosort, files_only;
  54. {
  55.   /* !!#### this function has not been Mule-ized */
  56.   /* This function can GC */
  57.   DIR *d;
  58.   int dirname_length;
  59.   Lisp_Object list, name, dirfilename;
  60.   Lisp_Object handler;
  61.  
  62.   char statbuf [MAXNAMLEN+2];
  63.   char *statbuf_tail;
  64.   Lisp_Object tail_cons;
  65.   char slashfilename[MAXNAMLEN+2];
  66.   char *filename = slashfilename;
  67.  
  68.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  69.  
  70.   /* #### Needs more gcpro's */
  71.   GCPRO4 (dirname, match, files_only, tail_cons);
  72.  
  73.   /* If the file name has special constructs in it,
  74.      call the corresponding file handler.  */
  75.   handler = Ffind_file_name_handler (dirname, Qdirectory_files);
  76.   if (!NILP (handler))
  77.   {
  78.     UNGCPRO;
  79.     if (!NILP (files_only))
  80.       return call6 (handler, Qdirectory_files, dirname, full, match, nosort,
  81.                     files_only);
  82.     else
  83.       return call5 (handler, Qdirectory_files, dirname, full, match,
  84.             nosort);
  85.   }
  86.  
  87.   dirname = Fexpand_file_name (dirname, Qnil);
  88.   dirfilename = Fdirectory_file_name (dirname);
  89.  
  90.   {
  91.     /* XEmacs: this should come before the opendir() because it might error. */
  92.     /*#### need more gcpro's */
  93.     Lisp_Object name_as_dir = Ffile_name_as_directory (dirname);
  94.     CHECK_STRING (name_as_dir, 0);
  95.     memcpy (statbuf, ((char *) string_data (XSTRING (name_as_dir))),
  96.            string_length (XSTRING (name_as_dir)));
  97.     statbuf_tail = statbuf + string_length (XSTRING (name_as_dir));
  98.   }
  99.  
  100.   /* XEmacs: this must come after Ffile_name_as_directory() or searchbuf
  101.      gets smashed.   This should come before the opendir() because it
  102.      might signal an error.
  103.    */
  104.   if (!NILP (match))
  105.     {
  106.       CHECK_STRING (match, 3);
  107.  
  108.       /* MATCH might be a flawed regular expression.  Rather than
  109.      catching and signalling our own errors, we just call
  110.      compile_pattern to do the work for us.  */
  111. #ifdef VMS
  112.       compile_pattern (match, &searchbuf, 0,
  113.                string_data
  114.                (XSTRING (XBUFFER (Vbuffer_defaults)->downcase_table)),
  115.                0, 0);
  116. #else
  117.       compile_pattern (match, &searchbuf, 0, 0, 0, 0);
  118. #endif
  119.     }
  120.  
  121.   /* Now searchbuf is the compiled form of MATCH; don't call anything
  122.      which might compile a new regexp until we're done with the loop!  */
  123.  
  124.  
  125.   /* Do this opendir after anything which might signal an error; if
  126.      an error is signalled while the directory stream is open, we
  127.      have to make sure it gets closed, and setting up an
  128.      unwind_protect to do so would be a pain.  */
  129.   d = opendir ((char *) string_data (XSTRING (dirfilename)));
  130.   if (! d)
  131.     report_file_error ("Opening directory", list1 (dirname));
  132.  
  133.   list = Qnil;
  134.   tail_cons = Qnil;
  135.   dirname_length = string_length (XSTRING (dirname));
  136. #ifndef VMS
  137.   if (dirname_length == 0
  138.       || string_char (XSTRING (dirname), dirname_length - 1) != '/')
  139.   {
  140.     *filename++ = '/';
  141.     dirname_length++;
  142.   }
  143. #endif /* VMS */
  144.  
  145.   /* Loop reading blocks */
  146.   while (1)
  147.     {
  148.       DIRENTRY *dp = readdir (d);
  149.       int len;
  150.  
  151.       if (!dp) break;
  152.       len = NAMLEN (dp);
  153.       if (DIRENTRY_NONEMPTY (dp))
  154.     {
  155.       int result;
  156.       Lisp_Object oinhibit_quit = Vinhibit_quit;
  157.       strncpy (filename, dp->d_name, len);
  158.       filename[len] = 0;
  159.       /* re_search can now QUIT, so prevent it to avoid
  160.          filedesc lossage */
  161.       Vinhibit_quit = Qt;
  162.       result = (NILP (match)
  163.           || (0 <= re_search (&searchbuf, filename, len, 0, len, 0)));
  164.           Vinhibit_quit = oinhibit_quit;
  165.           if (result)
  166.         {
  167.           if (!NILP (files_only))
  168.         {
  169.           int dir_p;
  170.           struct stat st;
  171.  
  172.           memcpy (statbuf_tail, filename, len);
  173.           statbuf_tail [len] = 0;
  174.  
  175.           if (stat (statbuf, &st) < 0)
  176.             dir_p = 0;
  177.           else
  178.             dir_p = ((st.st_mode & S_IFMT) == S_IFDIR);
  179.  
  180.           if (EQ (files_only, Qt) && dir_p)
  181.             continue;
  182.           else if (!EQ (files_only, Qt) && !dir_p)
  183.             continue;
  184.         }
  185.  
  186.           if (!NILP (full))
  187.         name = concat2 (dirname, build_string (slashfilename));
  188.           else
  189.         name = make_string ((Bufbyte *) filename, len);
  190.  
  191.           if (NILP (tail_cons))
  192.         {
  193.           list = list1 (name);
  194.           tail_cons = list;
  195.         }
  196.           else
  197.         {
  198.           XCDR (tail_cons) = list1 (name);
  199.           tail_cons = XCDR (tail_cons);
  200.         }
  201.         }
  202.     }
  203.     }
  204.   closedir (d);
  205.   UNGCPRO;
  206.   if (!NILP (nosort))
  207.     return list;
  208.   return Fsort (Fnreverse (list), Qstring_lessp);
  209. }
  210.  
  211. static Lisp_Object file_name_completion (Lisp_Object file, 
  212.                                          Lisp_Object dirname, 
  213.                                          int all_flag, int ver_flag);
  214.  
  215. DEFUN ("file-name-completion", Ffile_name_completion, Sfile_name_completion,
  216.   2, 2, 0,
  217.   "Complete file name FILE in directory DIR.\n\
  218. Returns the longest string common to all filenames in DIR\n\
  219. that start with FILE.\n\
  220. If there is only one and FILE matches it exactly, returns t.\n\
  221. Returns nil if DIR contains no name starting with FILE.\n\
  222. \n\
  223. Filenames which end with any member of `completion-ignored-extensions'\n\
  224. are not considered as possible completions for FILE unless there is no\n\
  225. other possible completion.  `completion-ignored-extensions' is not applied\n\
  226. to the names of directories.")
  227.   (file, dirname)
  228.      Lisp_Object file, dirname;
  229. {
  230.   /* This function can GC */
  231.   Lisp_Object handler;
  232.   /* Don't waste time trying to complete a null string.
  233.      Besides, this case happens when user is being asked for
  234.      a directory name and has supplied one ending in a /.
  235.      We would not want to add anything in that case
  236.      even if there are some unique characters in that directory.  */
  237.   if (STRINGP (file) && string_length (XSTRING (file)) == 0)
  238.     return file;
  239.  
  240.   /* If the file name has special constructs in it,
  241.      call the corresponding file handler.  */
  242.   handler = Ffind_file_name_handler (dirname, Qfile_name_completion);
  243.   if (!NILP (handler))
  244.     return call3 (handler, Qfile_name_completion, file, dirname);
  245.  
  246.   return file_name_completion (file, dirname, 0, 0);
  247. }
  248.  
  249. DEFUN ("file-name-all-completions", Ffile_name_all_completions,
  250.   Sfile_name_all_completions, 2, 2, 0,
  251.   "Return a list of all completions of file name FILE in directory DIR.\n\
  252. These are all file names in directory DIR which begin with FILE.\n\
  253. \n\
  254. Filenames which end with any member of `completion-ignored-extensions'\n\
  255. are not considered as possible completions for FILE unless there is no\n\
  256. other possible completion.  `completion-ignored-extensions' is not applied\n\
  257. to the names of directories.")
  258.   (file, dirname)
  259.      Lisp_Object file, dirname;
  260. {
  261.   /* This function can GC */
  262.   Lisp_Object handler;
  263.  
  264.   /* If the file name has special constructs in it,
  265.      call the corresponding file handler.  */
  266.   handler = Ffind_file_name_handler (dirname, Qfile_name_all_completions);
  267.   if (!NILP (handler))
  268.     return call3 (handler, Qfile_name_all_completions, file,
  269.           dirname);
  270.  
  271.   return file_name_completion (file, dirname, 1, 0);
  272. }
  273.  
  274. static int
  275. file_name_completion_stat (Lisp_Object dirname, DIRENTRY *dp,
  276.                struct stat *st_addr)
  277. {
  278.   Bytecount len = NAMLEN (dp);
  279.   Bytecount pos = string_length (XSTRING (dirname));
  280.   int value;
  281.   char *fullname = (char *) alloca (len + pos + 2);
  282.  
  283.   memcpy (fullname, string_data (XSTRING (dirname)), pos);
  284. #ifndef VMS
  285.   if (fullname[pos - 1] != '/')
  286.     fullname[pos++] = '/';
  287. #endif
  288.  
  289.   memcpy (fullname + pos, dp->d_name, len);
  290.   fullname[pos + len] = 0;
  291.  
  292. #ifdef S_IFLNK
  293.   /* We want to return success if a link points to a nonexistent file,
  294.      but we want to return the status for what the link points to,
  295.      in case it is a directory.  */
  296.   value = lstat (fullname, st_addr);
  297.   if (S_ISLNK (st_addr->st_mode))
  298.     stat (fullname, st_addr);
  299. #else
  300.   value = stat (fullname, st_addr);
  301. #endif
  302.   return (value);
  303. }
  304.  
  305. static Lisp_Object
  306. file_name_completion (Lisp_Object file, Lisp_Object dirname, int all_flag,
  307.               int ver_flag)
  308. {
  309.   /* This function can GC */
  310.   DIR *d = 0;
  311.   int matchcount = 0;
  312.   Lisp_Object bestmatch = Qnil;
  313.   Charcount bestmatchsize = 0;
  314.   struct stat st;
  315.   int passcount;
  316.   int speccount = specpdl_depth ();
  317.   Charcount file_name_length;
  318.   DIRENTRY *((*readfunc) (DIR *)) = readdir;
  319.   struct gcpro gcpro1, gcpro2, gcpro3;
  320.  
  321.   GCPRO3 (file, dirname, bestmatch);
  322.  
  323.   CHECK_STRING (file, 0);
  324.  
  325. #ifdef VMS
  326.   /* Filename completion on VMS ignores case, since VMS filesys does.  */
  327.   specbind (Qcompletion_ignore_case, Qt);
  328.  
  329.   if (ver_flag)
  330.     readfunc = readdirver;
  331. #endif /* VMS */
  332.  
  333. #ifdef FILE_SYSTEM_CASE
  334.   file = FILE_SYSTEM_CASE (file);
  335. #endif
  336.   dirname = Fexpand_file_name (dirname, Qnil);
  337.   bestmatch = Qnil;
  338.   file_name_length = string_char_length (XSTRING (file));
  339.  
  340.   /* With passcount = 0, ignore files that end in an ignored extension.
  341.      If nothing found then try again with passcount = 1, don't ignore them.
  342.      If looking for all completions, start with passcount = 1,
  343.      so always take even the ignored ones.
  344.  
  345.      ** It would not actually be helpful to the user to ignore any possible
  346.      completions when making a list of them.**  */
  347.  
  348.   for (passcount = !!all_flag; NILP (bestmatch) && passcount < 2; passcount++)
  349.     {
  350.       d = opendir ((char *)
  351.            string_data (XSTRING (Fdirectory_file_name (dirname))));
  352.       if (!d)
  353.     report_file_error ("Opening directory", list1 (dirname));
  354.  
  355.       /* Loop reading blocks */
  356.       /* (att3b compiler bug requires do a null comparison this way) */
  357.       while (1)
  358.     {
  359.       DIRENTRY *dp;
  360.       Bytecount len;
  361.       /* scmp() works in characters, not bytes, so we have to compute
  362.          this value: */
  363.       Charcount cclen;
  364.           int directoryp;
  365.           int ignored_extension_p = 0;
  366.       Bufbyte *d_name;
  367.  
  368.       dp = (*readfunc) (d);
  369.       if (!dp) break;
  370.  
  371.       d_name = (Bufbyte *) dp->d_name;
  372.       len = NAMLEN (dp);
  373.       cclen = bytecount_to_charcount (d_name, len);
  374.  
  375.       /* Can't just use QUIT because we have to make sure the file
  376.              descriptor gets closed. */
  377.       if (QUITP)
  378.         {
  379.           closedir (d);
  380.           signal_quit ();
  381.         }
  382.  
  383.       if (! DIRENTRY_NONEMPTY (dp)
  384.           || cclen < file_name_length
  385.           || 0 <= scmp (d_name,
  386.                 string_data (XSTRING (file)),
  387.                 file_name_length))
  388.         continue;
  389.  
  390.           if (file_name_completion_stat (dirname, dp, &st) < 0)
  391.             continue;
  392.  
  393.           directoryp = ((st.st_mode & S_IFMT) == S_IFDIR);
  394.           if (!directoryp)
  395.             {
  396.           /* Compare extensions-to-be-ignored against end of this file name */
  397.           /* if name is not an exact match against specified string */
  398.           if (!passcount && cclen > file_name_length)
  399.         {
  400.           Lisp_Object tem;
  401.           /* and exit this for loop if a match is found */
  402.           for (tem = Vcompletion_ignored_extensions;
  403.                CONSP (tem);
  404.                        tem = XCDR (tem))
  405.             {
  406.               Lisp_Object elt = XCAR (tem);
  407.               Charcount skip;
  408.  
  409.               if (!STRINGP (elt)) continue;
  410.               skip = cclen - string_char_length (XSTRING (elt));
  411.               if (skip < 0) continue;
  412.  
  413.               if (0 > scmp (charptr_addr (d_name, skip),
  414.                     string_data (XSTRING (elt)),
  415.                     string_char_length (XSTRING (elt))))
  416.             {
  417.               ignored_extension_p = 1;
  418.               break;
  419.             }
  420.             }
  421.         }
  422.         }
  423.  
  424.       /* If an ignored-extensions match was found,
  425.          don't process this name as a completion.  */
  426.       if (!passcount && ignored_extension_p)
  427.         continue;
  428.  
  429.       if (!passcount && regexp_ignore_completion_p (d_name, Qnil, 0, len))
  430.             continue;
  431.  
  432.           /* Update computation of how much all possible completions match */
  433.           matchcount++;
  434.  
  435.           if (all_flag || NILP (bestmatch))
  436.             {
  437.               Lisp_Object name = Qnil;
  438.               struct gcpro gcpro1;
  439.               GCPRO1 (name);
  440.               /* This is a possible completion */
  441.               if (directoryp)
  442.                 {
  443.                   /* This completion is a directory; make it end with '/' */
  444.                   name = Ffile_name_as_directory
  445.             (make_string (d_name, len));
  446.                 }
  447.               else
  448.                 name = make_string (d_name, len);
  449.               if (all_flag)
  450.                 {
  451.                   bestmatch = Fcons (name, bestmatch);
  452.                 }
  453.               else
  454.                 {
  455.                   bestmatch = name;
  456.                   bestmatchsize = string_char_length (XSTRING (name));
  457.                 }
  458.               UNGCPRO;
  459.             }
  460.           else
  461.             {
  462.               Charcount compare = min (bestmatchsize, cclen);
  463.               Bufbyte *p1 = string_data (XSTRING (bestmatch));
  464.               Bufbyte *p2 = d_name;
  465.               Charcount matchsize = scmp (p1, p2, compare);
  466.  
  467.               if (matchsize < 0)
  468.                 matchsize = compare;
  469.               if (completion_ignore_case)
  470.                 {
  471.                   /* If this is an exact match except for case,
  472.                      use it as the best match rather than one that is not
  473.                      an exact match.  This way, we get the case pattern
  474.                      of the actual match.  */
  475.                   if ((matchsize == len
  476.                        && matchsize + !!directoryp 
  477.                        < string_char_length (XSTRING (bestmatch)))
  478.                       ||
  479.                       /* If there is no exact match ignoring case,
  480.                          prefer a match that does not change the case
  481.                          of the input.  */
  482.                       (((matchsize == len)
  483.                         ==
  484.                         (matchsize + !!directoryp 
  485.                          == string_char_length (XSTRING (bestmatch))))
  486.                        /* If there is more than one exact match aside from
  487.                           case, and one of them is exact including case,
  488.                           prefer that one.  */
  489.                        && 0 > scmp_1 (p2, string_data (XSTRING (file)),
  490.                       file_name_length, 0)
  491.                        && 0 <= scmp_1 (p1, string_data (XSTRING (file)),
  492.                        file_name_length, 0)))
  493.                     {
  494.                       bestmatch = make_string (d_name, len);
  495.                       if (directoryp)
  496.                         bestmatch =
  497.               Ffile_name_as_directory (bestmatch);
  498.                     }
  499.                 }
  500.  
  501.               /* If this dirname all matches,
  502.                  see if implicit following slash does too.  */
  503.               if (directoryp
  504.                   && compare == matchsize
  505.                   && bestmatchsize > matchsize
  506.                   && charptr_char (p1, matchsize) == '/')
  507.                 matchsize++;
  508.               bestmatchsize = matchsize;
  509.             }
  510.         }
  511.       closedir (d);
  512.     }
  513.  
  514.   unbind_to (speccount, Qnil);
  515.  
  516.   UNGCPRO;
  517.  
  518.   if (all_flag || NILP (bestmatch))
  519.     return bestmatch;
  520.   if (matchcount == 1 && bestmatchsize == file_name_length)
  521.     return Qt;
  522.   return Fsubstring (bestmatch, make_number (0), make_number (bestmatchsize));
  523. }
  524.  
  525.  
  526. Lisp_Object
  527. make_directory_hash_table (char *path)
  528. {
  529.   DIR *d;
  530.   DIRENTRY *dp;
  531.   Bytecount len;
  532.   Lisp_Object hash = make_lisp_hashtable (100, lisp_string_equal,
  533.                       lisp_string_hash, HASHTABLE_NONWEAK);
  534.   if ((d = opendir (path)))
  535.     {
  536.       while ((dp = readdir (d)))
  537.     {
  538.       len = NAMLEN (dp);
  539.       if (DIRENTRY_NONEMPTY (dp))
  540.         Fputhash (make_string ((Bufbyte *) dp->d_name, len), Qt, hash);
  541.     }
  542.       closedir (d);
  543.     }
  544.   return hash;
  545. }
  546.  
  547. #ifdef VMS
  548.  
  549. DEFUN ("file-name-all-versions", Ffile_name_all_versions,
  550.   Sfile_name_all_versions, 2, 2, 0,
  551.   "Return a list of all versions of file name FILE in directory DIR.")
  552.   (file, dirname)
  553.      Lisp_Object file, dirname;
  554. {
  555.   /* This function can GC */
  556.   return file_name_completion (file, dirname, 1, 1);
  557. }
  558.  
  559. DEFUN ("file-version-limit", Ffile_version_limit, Sfile_version_limit, 1, 1, 0,
  560.   "Return the maximum number of versions allowed for FILE.\n\
  561. Returns nil if the file cannot be opened or if there is no version limit.")
  562.   (filename)
  563.      Lisp_Object filename;
  564. {
  565.   /* This function can GC */
  566.   Lisp_Object retval;
  567.   struct FAB    fab;
  568.   struct RAB    rab;
  569.   struct XABFHC xabfhc;
  570.   int status;
  571.  
  572.   filename = Fexpand_file_name (filename, Qnil);
  573.   CHECK_STRING (filename, 0);
  574.   fab      = cc$rms_fab;
  575.   xabfhc   = cc$rms_xabfhc;
  576.   fab.fab$l_fna = string_data (XSTRING (filename));
  577.   fab.fab$b_fns = strlen (fab.fab$l_fna);
  578.   fab.fab$l_xab = (char *) &xabfhc;
  579.   status = sys$open (&fab, 0, 0);
  580.   if (status != RMS$_NORMAL)    /* Probably non-existent file */
  581.     return Qnil;
  582.   sys$close (&fab, 0, 0);
  583.   if (xabfhc.xab$w_verlimit == 32767)
  584.     return Qnil;        /* No version limit */
  585.   else
  586.     return make_number (xabfhc.xab$w_verlimit);
  587. }
  588.  
  589. #endif /* VMS */
  590.  
  591.  
  592. static Lisp_Object
  593. wasteful_word_to_lisp (unsigned int item)
  594. {
  595.   /* Compatibility: in other versions, file-attributes returns a LIST
  596.      of two 16 bit integers... */
  597.   Lisp_Object cons = word_to_lisp (item);
  598.   XCDR (cons) = Fcons (XCDR (cons), Qnil);
  599.   return cons;
  600. }
  601.  
  602. DEFUN ("file-attributes", Ffile_attributes, Sfile_attributes, 1, 1, 0,
  603.   "Return a list of attributes of file FILENAME.\n\
  604. Value is nil if specified file cannot be opened.\n\
  605. Otherwise, list elements are:\n\
  606.  0. t for directory, string (name linked to) for symbolic link, or nil.\n\
  607.  1. Number of links to file.\n\
  608.  2. File uid.\n\
  609.  3. File gid.\n\
  610.  4. Last access time, as a list of two integers.\n\
  611.   First integer has high-order 16 bits of time, second has low 16 bits.\n\
  612.  5. Last modification time, likewise.\n\
  613.  6. Last status change time, likewise.\n\
  614.  7. Size in bytes. (-1, if number is out of range).\n\
  615.  8. File modes, as a string of ten letters or dashes as in ls -l.\n\
  616.  9. t iff file's gid would change if file were deleted and recreated.\n\
  617. 10. inode number.\n\
  618. 11. Device number.\n\
  619. \n\
  620. If file does not exist, returns nil.")
  621.   (filename)
  622.      Lisp_Object filename;
  623. {
  624.   /* This function can GC */
  625.   Lisp_Object values[12];
  626.   Lisp_Object dirname = Qnil;
  627.   struct stat s;
  628.   char modes[10];
  629.   Lisp_Object handler;
  630.   struct gcpro gcpro1, gcpro2;
  631.  
  632.   GCPRO1 (filename);
  633.   filename = Fexpand_file_name (filename, Qnil);
  634.  
  635.   /* If the file name has special constructs in it,
  636.      call the corresponding file handler.  */
  637.   handler = Ffind_file_name_handler (filename, Qfile_attributes);
  638.   UNGCPRO;
  639.   if (!NILP (handler))
  640.     return call2 (handler, Qfile_attributes, filename);
  641.  
  642.   if (lstat ((char *) string_data (XSTRING (filename)), &s) < 0)
  643.     return Qnil;
  644.  
  645.   GCPRO2 (filename, dirname);
  646.  
  647. #ifdef BSD4_2
  648.   dirname = Ffile_name_directory (filename);
  649. #endif
  650.  
  651. #ifdef MSDOS
  652.   {
  653.     char *tmpnam =
  654.       (char *) string_data (XSTRING (Ffile_name_nondirectory (filename)));
  655.     int l = strlen (tmpnam);
  656.  
  657.     if (l >= 5 
  658.     && S_ISREG (s.st_mode)
  659.     && (stricmp (&tmpnam[l - 4], ".com") == 0
  660.         || stricmp (&tmpnam[l - 4], ".exe") == 0
  661.         || stricmp (&tmpnam[l - 4], ".bat") == 0))
  662.       {
  663.     s.st_mode |= S_IEXEC;
  664.       }
  665.   }
  666. #endif /* MSDOS */
  667.  
  668.   switch (s.st_mode & S_IFMT)
  669.     {
  670.     default:
  671.       values[0] = Qnil;
  672.       break;
  673.     case S_IFDIR:
  674.       values[0] = Qt;
  675.       break;
  676. #ifdef S_IFLNK
  677.     case S_IFLNK:
  678.       values[0] = Ffile_symlink_p (filename);
  679.       break;
  680. #endif
  681.     }
  682.   values[1] = make_number (s.st_nlink);
  683.   values[2] = make_number (s.st_uid);
  684.   values[3] = make_number (s.st_gid);
  685.   values[4] = wasteful_word_to_lisp (s.st_atime);
  686.   values[5] = wasteful_word_to_lisp (s.st_mtime);
  687.   values[6] = wasteful_word_to_lisp (s.st_ctime);
  688.   values[7] = make_number ((LISP_WORD_TYPE) s.st_size);
  689.   /* If the size is out of range, give back -1.  */
  690.   /* #### Fix when Emacs gets bignums! */
  691.   if (XINT (values[7]) != s.st_size)
  692.     XSETINT (values[7], -1);
  693.   filemodestring (&s, modes);
  694.   values[8] = make_string ((Bufbyte *) modes, 10);
  695. #if defined (BSD4_2) || defined (BSD4_3)    /* file gid will be dir gid */
  696.   {
  697.     struct stat sdir;
  698.  
  699.     if (!NILP (dirname) && stat ((char *) string_data (XSTRING (dirname)), &sdir) == 0)
  700.       values[9] = (sdir.st_gid != s.st_gid) ? Qt : Qnil;
  701.     else                        /* if we can't tell, assume worst */
  702.       values[9] = Qt;
  703.   }
  704. #else                           /* file gid will be egid */
  705.   values[9] = (s.st_gid != getegid ()) ? Qt : Qnil;
  706. #endif    /* BSD4_2 or BSD4_3 */
  707.   values[10] = make_number (s.st_ino);
  708.   values[11] = make_number (s.st_dev);
  709.   UNGCPRO;
  710.   return Flist (countof (values), values);
  711. }
  712.  
  713.  
  714. /************************************************************************/
  715. /*                            initialization                            */
  716. /************************************************************************/
  717.  
  718. void
  719. syms_of_dired (void)
  720. {
  721.   defsymbol (&Qdirectory_files, "directory-files");
  722.   defsymbol (&Qfile_name_completion, "file-name-completion");
  723.   defsymbol (&Qfile_name_all_completions, "file-name-all-completions");
  724.   defsymbol (&Qfile_attributes, "file-attributes");
  725.  
  726.   defsubr (&Sdirectory_files);
  727.   defsubr (&Sfile_name_completion);
  728. #ifdef VMS
  729.   defsubr (&Sfile_name_all_versions);
  730.   defsubr (&Sfile_version_limit);
  731. #endif /* VMS */
  732.   defsubr (&Sfile_name_all_completions);
  733.   defsubr (&Sfile_attributes);
  734. }
  735.  
  736. void
  737. vars_of_dired (void)
  738. {
  739.   DEFVAR_LISP ("completion-ignored-extensions", &Vcompletion_ignored_extensions,
  740.     "*Completion ignores filenames ending in any string in this list.\n\
  741. This variable does not affect lists of possible completions,\n\
  742. but does affect the commands that actually do completions.\n\
  743. It is used by the functions `file-name-completion' and\n\
  744. `file-name-all-completions'.");
  745.   Vcompletion_ignored_extensions = Qnil;
  746. }
  747.